<?php

namespace WeForge\Runtime\Fpm;

use Symfony\Component\HttpFoundation\Response;

class FpmResponse
{
    /**
     * The fastcgi response instance.
     *
     * @var \Swoole\FastCGI\HttpResponse
     */
    protected $response;

    /**
     * @var array
     */
    protected $statusTexts = [
        419 => 'Authentication Timeout',
    ];

    /**
     * Create a new FpmResponse instance.
     *
     * @param \Swoole\FastCGI\HttpResponse $response
     */
    public function __construct($response)
    {
        $this->response = $response;
    }

    /**
     * @param \Swoole\Http\Response $response
     */
    public function send($response)
    {
        $response->header = $this->response->getHeaders();

        $status = $this->response->getStatusCode();

        if (isset(Response::$statusTexts[$status])) {
            $response->status($status);
        } else {
            $response->status(
                $status,
                $this->statusTexts[$status] ?? 'Unknown'
            );
        }

        $response->cookie = $this->response->getSetCookieHeaderLines();
        $response->end($this->response->getBody());
    }
}
